<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>主页</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/plugins/build/css/app.css" media="all">
</head>
<body>
<div class="container">
    <form class="layui-form custom_search_form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">文章标题</label>
            <div class="layui-input-block">
                <input type="text" name="contentTitle" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="custom_btn_search">
            <button class="layui-btn" lay-submit="" lay-filter="search">搜索</button>
            <a class="layui-btn layui-btn-primary layui-btn-sm" href="edit.html">
                <i class="layui-icon">&#xe654;</i>
            </a>
        </div>
    </form>
    <table id="table" lay-filter="table"></table>
</div>
<script src="/plugins/layui/layui.js"></script>
<script src="/plugins/date/date.js"></script>
<script src="/plugins/common.js"></script>
<script>
    //配置项：列表地址、删除地址、编辑页面、详情页面
    var listUrl = SERVER_URL+"/cmsContent/list";
    var delUrl = SERVER_URL+"/cmsContent/del";
    var editUrl = "edit.html";
    var detailUrl = "detail.html";

    //配置项：列表高度
    var tableHeight = "full-140";

    //配置项：主键名称
    var ID = "contentId";

    layui.use(['jquery', 'table', 'form','layer'], function() {
        var $ = layui.jquery;
        var form = layui.form;
        var table = layui.table;
        layer.config({
            anim: 5
            ,offset: '50px'
            ,maxmin: true
            ,isOutAnim: false
        })

        //配置项：列表项
        table.render({
            elem: '#table'
            ,height: tableHeight
            ,url: listUrl
            ,page: true
            ,cols: [[
                {field: 'contentTitle', title: '文章标题'},
                {field: 'createTime', title: '发布时间', templet:'#createTime'},
                {fixed: 'right', title: '操作', width:150, toolbar: '#toolBar'},
            ]]
        });

        //列表行末工具栏
        table.on('tool(table)', function(obj){
            if(obj.event === 'del'){
                layer.confirm('确定删除？', {
                    btn: ['确定','取消']
                }, function(){
                    layer.load(2, {time: 5*1000});
                    $.ajax({
                        type: 'POST',
                        url: delUrl,
                        data: {id: obj.data[ID]},
                        success: function (result) {
                            layer.closeAll();
                            if(result.code == 20000){
                                layer.msg('操作成功', {icon: 1,time: 1000}, function(){
                                    window.location.reload();
                                })
                            }else{
                                layer.msg(result.msg, {time: 1500});
                            }
                        }
                    });
                }, function(){
                    layer.closeAll();
                });
            }
        });

        //条件搜索
        form.on('submit(search)', function(data){
            table.reload('table', {
                url: listUrl
                ,where: data.field
                ,page:{ curr: 1 }
                ,height: tableHeight
            });
            return false;
        });
    });

    //刷新列表
    function reload() {
        layer.closeAll();
        layer.msg('操作成功', {icon: 1,time: 1500}, function(){
            window.location.reload();
        })
    }
</script>
<script type="text/html" id="toolBar">
    <div class="layui-btn-group custom_btn_group">
        <a class="layui-btn layui-btn-primary layui-btn-sm" href="detail.html?id={{d.contentId}}">
            <i class="layui-icon">&#xe63e;</i>
        </a>
        <a class="layui-btn layui-btn-primary layui-btn-sm" href="edit.html?id={{d.contentId}}">
            <i class="layui-icon">&#xe642;</i>
        </a>
        <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="del">
            <i class="layui-icon">&#xe640;</i>
        </button>
    </div>
</script>
<script type="text/html" id="createTime">
    {{#
        var date = new Date();
        date.setTime(d.createTime);
        return date.Format("yyyy-MM-dd hh:mm:ss");
    }}
</script>
</body>
</html>
